import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
   
    component: () => import(/* webpackChunkName: "home" */ '../page/Home.vue'),
    children:[
      {
        path: '/exercises',
       
        component: () => import(/* webpackChunkName: "execise" */ '../views/exercises.vue')
      },
      {
        path: '/complaint',
       
        component: () => import(/* webpackChunkName: "comp" */ '../views/complaint.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "login" */ '../page/login.vue'),

  },
  {
    //没匹配到就回到主页
    path:'*',redirect:'/home'
  },
  {

    path:'/',redirect:'/home'
  }
]

const router = new VueRouter({
  routes
})

//导航守卫
router.beforeEach((to, from, next) => {
  //to 访问路径
  //from 代表从个路径来
  //next 放行

  //注册和登录页面可以放行
  if (to.path == '/login') return next()
 
      //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
